﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace FastNET.CleanAct.Common.Configuration.Configuration
{
    [XmlType(TypeName = "jobConfig", Namespace = JobConfigDeclarations.SchemaVersion)]
    [XmlRoot(ElementName = "config", Namespace = JobConfigDeclarations.SchemaVersion, IsNullable = false)]
    [Serializable]
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    public class JobConfigSettings
    {
        public JobConfigSettings()
        {
            Jobs = new List<JobItem>();
        }

        [XmlElement(ElementName = "job")]
        public List<JobItem> Jobs { get; set; }

        #region Loading/Saving

        /// <summary>
        /// Loads a ActionConfigSettings from the specified file
        /// </summary>
        /// <param name="file">File to load the config from</param>
        /// <returns>ActionConfigSettings with the XML deserialized classes</returns>
        public static JobConfigSettings Load(string file)
        {
            XmlSerializer s = new XmlSerializer(typeof(JobConfigSettings));

            using (FileStream fileStream = new FileStream(file, FileMode.Open))
            {
                return (JobConfigSettings)s.Deserialize(fileStream);
            }
        }

        /// <summary>
        /// Loads a ActionConfigSettings from the specified XML Content
        /// </summary>
        /// <param name="content">XML to load the config from</param>
        /// <returns>ActionConfigSettings with the XML deserialized classes</returns>
        public static JobConfigSettings LoadXml(string content)
        {
            XmlSerializer s = new XmlSerializer(typeof(JobConfigSettings));

            using (TextReader reader = new StringReader(content))
            {
                return (JobConfigSettings)s.Deserialize(reader);
            }
        }

        /// <summary>
        /// Writes a ActionConfigSettings to a string of XML
        /// </summary>
        /// <param name="config">ActionConfigSettings to save</param>
        /// <returns>XML representation of the ActionConfigSettings</returns>
        public static string WriteXML(JobConfigSettings config)
        {
            XmlSerializer s = new XmlSerializer(typeof(JobConfigSettings));
            StringBuilder returnValue = new StringBuilder();

            using (TextWriter writer = new StringWriter(returnValue))
            {
                s.Serialize(writer, config);
                return returnValue.ToString();
            }
        }

        /// <summary>
        /// Saves a ActionConfigSettings to the specified file
        /// </summary>
        /// <param name="file">File to save the config to</param>
        /// <param name="config">ActionConfigSettings to save</param>
        public static void Save(string file, JobConfigSettings config)
        {
            XmlSerializer s = new XmlSerializer(typeof(JobConfigSettings));

            using (FileStream fileStream = new FileStream(file, FileMode.Open))
            {
                s.Serialize(fileStream, config);
            }
        }

        /// <summary>
        /// Saves a ActionConfigSettings to the specified file
        /// </summary>
        /// <param name="file">File to save the config to</param>
        public void Save(string file)
        {
            Save(file, this);
        }

        #endregion
    }
}
